package com.xidian.yunwudemo.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xidian.yunwudemo.common.Result;
import com.xidian.yunwudemo.dto.AuthUserInfoDto;
import com.xidian.yunwudemo.entities.DataUserAuth;
import com.xidian.yunwudemo.entities.PersonalUser;
import com.xidian.yunwudemo.service.UserService43Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("userServiceData")
public class UserAuthController {
    @Autowired
    UserService43Service userService43Service;


    // 以下为个人用户请求链接
    // 获取全部的个人信息
    @GetMapping("/personalUsers/list")
    public Result listAllPersonalUsersInfo(@RequestParam("pageNum")Integer pageNum,
                                           @RequestParam("pageSize") Integer pageSize,
                                           @RequestParam(value = "userName", required=false) String userName,
                                           @RequestParam(value = "sort", required = false) String sort){
        String orderBy =  "";
        PageHelper.startPage(pageNum,pageSize);
        if(userName == null){

            List<AuthUserInfoDto> res = userService43Service.listAllPersonalUsersInfo();
            PageInfo<AuthUserInfoDto> info = new PageInfo<>(res);
            return Result.OK(info);
        }else{
            if(sort.trim().equals("-id")){
                orderBy = "id DESC";
            }else {
                orderBy = "id ASC";
            }
            List<AuthUserInfoDto> res = userService43Service.filterByName(userName);
            PageInfo<AuthUserInfoDto> info = new PageInfo<>(res);
            return Result.OK(info);
        }
    }

    // 查找某一个人信息，主要用户编辑时显示对应表项的信息
    // TODO:此处应该使用dto去接收查询结果，此处先更正
    @GetMapping("/personalUser/detail")
    public Result getPersonalUserInfoById(@RequestParam("id") Integer id){
        AuthUserInfoDto res = userService43Service.getPersonalUserInfoById(id);
        return Result.OK(res);
    }
    // 添加用户 不用
    @PostMapping("/personalUsers/create")
    public Result getPersonalUserInfoById(@RequestBody PersonalUser user){
        boolean flag = userService43Service.addPersonalUserInfo(user);
        if(flag){
            return Result.OK("添加用户成功");
        }else{
            return Result.ERROR("添加用户失败");
        }
    }

    //更新用户信息
    @PostMapping("/personalUsers/update")
    public Result updatePersonalUserInfo(@RequestBody AuthUserInfoDto user){
        boolean flag = userService43Service.updatePersonalUserInfo(user);
        if(flag){
            return Result.OK("更新用户信息成功");
        }else{
            return Result.ERROR("更新用户信息失败");
        }
    }
    // TODO:支持多选删除
    // 删除选择条目的用户信息
    @GetMapping("/personalUser/delete")
    public Result deletePersonalUserInfo(@RequestParam("id") Integer id){
        boolean flag = userService43Service.deletePersonalUserInfo(id);
        if(flag){
            return Result.OK("删除用户成功");
        }else {
            return Result.ERROR("删除用户失败");
        }
    }

    //此部分为获得用户认证数据
    @GetMapping("/authData/list")
    public Result listAllAuthData(@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);
        List<DataUserAuth> data = userService43Service.listAllAuthData();
        PageInfo<DataUserAuth> res = new PageInfo<DataUserAuth>(data);
        return Result.OK(res);
    }

}
